Add conversions between sockets and multiaddrs#99
Add conversions between sockets and multiaddrs#99jmcph4 wants to merge 4 commits intomultiformats:masterfrom
Conversation
thomaseizinger
left a comment
There was a problem hiding this comment.
We should consider Udp as well and not just Tcp.
I think these would be better served as dedicated functions so we can include the tcp bit in the name to make this clear.
What is also not clear to me is what should happen with the remaining components of the multiaddr? Currently we just drop them. Should we return them instead?
I agree, I'll add the equivalent UDP methods and make the existing TCP ones dedicated methods rather than
Are you referring to the case where we need to produce a pub fn into_tcp_socket(&self) -> Option<(SocketAddr, Multiaddr)> {
/* snip */
Some((sock, remaining))
}Which would yield The issue with this is that it implicitly assumes a left-to-right parsing approach for what it means to convert a multiaddr into a |
Left-to-right is how you are meant to parse multiaddr, see https://github.com/multiformats/multiaddr?tab=readme-ov-file#interpreting-multiaddrs. Perhaps, the functions should be called something like: impl Multiaddr {
pub fn pop_front_tcp_socket(&mut self) -> Option<SocketAddr>;
}This modifies the |
|
Perhaps what we should add is a You should then be able to implement an efficient extension trait that uses Curious to hear what other maintainers think. |
| match socket { | ||
| SocketAddr::V4(sock) => Self::empty() | ||
| .with(Protocol::Ip4(*sock.ip())) | ||
| .with(Protocol::Tcp(sock.port())), |
There was a problem hiding this comment.
SocketAddr might as well specify a UDP address, thus this From implementation is not correct.
This PR allows two type conversions involving both
MultiaddrandSocketAddr:SocketAddrtoMultiaddrMultiaddrtoSocketAddr(as the space of all possible values of the former is obviously much larger than that for the latter)These are certainly fairly minor changes; however, they enable some niceties such as: